package writeor.aidraw.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import writeor.aidraw.model.AiImages;
import writeor.aidraw.model.User;
import writeor.aidraw.service.HomeService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HomeController {
    @Autowired
    private HomeService homeService;
//带分页信息的
    @RequestMapping(value = "/")
    public String getHotimagesForPage( @RequestParam(value = "page", defaultValue = "1") int page,
                                      @RequestParam(value = "count", defaultValue = "8") int count,
                                      HttpServletRequest request) {
        PageInfo<AiImages> HotimagesForPage=homeService.getHotImagesForPage(page,count);
        request.setAttribute("hotimages",HotimagesForPage);
        List imagelist = HotimagesForPage.getList();
        if(imagelist.size()>4){
            List hotimages1 = imagelist.subList(0, 4);
            List hotimages2 = imagelist.subList(4, imagelist.size());
            request.setAttribute("hotimages1",hotimages1);
            request.setAttribute("hotimages2",hotimages2);
        }else{
            request.setAttribute("hotimages1",imagelist);
        }

//        System.out.println("imagelist1+"+imagelist1);
//        System.out.println("imagelist2"+imagelist2);
        return "index";
    }


    //现在用的detail
    @RequestMapping(value = "/ImageDetail")//@PathVariable String image_path
    public String ImageDetail(@RequestParam(value = "img_path") String img_path,HttpServletRequest request) {
        System.out.println("String img_path路径是:"+img_path);

        //用路径名字获取图片的相关信息
        AiImages image_info=homeService.GetImageByPath(img_path);

        System.out.println("现在用的detail ImageDetail路径是:"+image_info.toString());

        //model.addAttribute("img_path",img_path);
        //model.addAttribute("image_info",image_info);
        request.setAttribute("img_path",img_path);
        request.setAttribute("image_info",image_info);
        return "Image_detail";
    }





    @RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)
    public String addUser4(@PathVariable String username,@PathVariable String password) {
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "demo/index";
    }

    @RequestMapping(value="/ImageDetail_v/{img_path}",method= RequestMethod.GET)
    public String deleteUser(@PathVariable String img_path,Model model) {
        System.out.println("String img_path:"+img_path);
        //model.addAttribute("img_path",img_path);
        //用路径名字获取图片的相关信息
        AiImages image_info=homeService.GetImageByPath(img_path);
        //model.addAttribute("img_path",img_path);
        //model.addAttribute("image_info",image_info);
        System.out.println("ImageDetail222222路径是:"+image_info.toString());
        //return "Image_detail";
        model.addAttribute("image_info",image_info);

        return "redirect:/ImageDetail?img_path="+img_path;
    }
    @RequestMapping("/To_image_detail")
    public String To_image_detail(HttpServletRequest request) {
        String img_path= (String) request.getAttribute("img_path");
        System.out.println("toImagedtail/img_path:"+img_path);
        return "Image_detail";
    }

    @GetMapping("/To_manageIndex")
    public String To_manageIndex(Model model, HttpSession session) {
        User user = (User) session.getAttribute("USER_SESSION");
        if(user.getUser_rank()!=1){
            return "redirect:/manage/image/my_images";
        }

        return "manage/manage_index";
    }
//    @RequestMapping("/")
//    public String index2(Model model){
//        List<AiImages> hotimages=homeService.getHotImages();
//        model.addAttribute("hotimages",hotimages);
//        System.out.print("hotimages"+hotimages);
//        return "index";
//    }
//    @RequestMapping("/index")
//    public String index(Model model){
//        List<AiImages> hotimages=homeService.getHotImages();
//        model.addAttribute("hotimages",hotimages);
//        System.out.print("hotimages"+hotimages);
//        return "index";
//    }
//
//    @GetMapping(value = "/")
//    public  String home(Model model){
//        List<AiImages> hotimages=homeService.getHotImages();
//        model.addAttribute("hotimages",hotimages);
//        System.out.print("hotimages"+hotimages);
//        return "index";
//    }

}
